using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Remove, "PsSpecUserPermission", SupportsShouldProcess = true, DefaultParameterSetName="ById")]
    public class RemovePsSpecUserPermissionCommand : PsSpecCommandBase
    {
        // ById Parameter Set
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = "ById")]
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                idSpecified = true;
            }
        }
        private int _id = -1;
        private bool idSpecified = false;

        // ByName Parameter Set
        [ValidateLength(1, 50)]
        [Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        public string ProjectName
        {
            get { return project; }
            set { project = value; }
        }
        private string project = "*";

        [Parameter(Mandatory = false, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        [ValidateLength(1, 50)]
        public String User
        {
            get
            {
                return _user;
            }
            set
            {
                _user = value;
            }
        }
        private string _user = "*";

        [Parameter(Mandatory = false, Position = 2, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        public PsSpecProjectRole Role
        {
            get
            {
                return _role;
            }
            set
            {
                _role = value;
            }
        }
        private PsSpecProjectRole _role = PsSpecProjectRole.Unspecified;

        protected override void ProcessRecord()
        {
            // Remove multiple cmdlets if specified by name
            if (! idSpecified)
            {
                foreach (PsSpecUserPermission user in GetUsers(ProjectName, User, Role))
                {
                    RemoveUser(user.Id);
                }
            }
            else
            {
                RemoveUser(Id);
            }
        }

        private IEnumerable<PsSpecUserPermission> GetUsers(string project, string user, PsSpecProjectRole role)
        {
            GetPsSpecUserPermissionCommand command = new GetPsSpecUserPermissionCommand();
            command.ServiceUri = ServiceUri;
            command.ProjectName = project;
            command.User = user;
            command.Role = role;

            foreach (Object result in command.Invoke())
            {
                yield return (PsSpecUserPermission)result;
            }
        }

        private void RemoveUser(int id)
        {
            if (ShouldProcess(
                String.Format("{0}", ServiceUri),
                String.Format(resources.RemoveProjectUserShouldProcess, id)))
            {            
                try
                {
                    Service.RemovePsSpecUserPermission(id);
                }
                catch (System.Web.Services.Protocols.SoapException e)
                {
                    WriteError(new ErrorRecord(e, "CMDLET_SERVICE_ERROR",
                                                ErrorCategory.InvalidOperation,
                                                String.Format(resources.RemoveProjectUserShouldProcess, id)));
                }
            }
        }
    }
}
